<?php

namespace app\api\controller\v1;

use app\api\model\Language as LanguageModel;
use app\api\model\LanguageQuestion;
use app\api\model\LanguageQuestion as LanguageQuestionModel;
use app\api\model\NeedQuestion as NeedQuestionModel;
use app\api\service\SearchService;
use app\api\validate\NeedQuestionValidate;
use app\common\controller\BaseController;
use app\api\model\LanguageGrade as LanguageGradeModel;

/**
 * 语种
 */
class Language extends BaseController
{
    protected $noNeedLogin = ['index', 'question', 'search'];
    /**
     * 首页语种
     */
    public function index()
    {
        $languages = LanguageModel::getHomeLanguage($this->userId);
        $this->success('', compact('languages'));
    }

    /**
     * 问卷需求
     */
    public function question()
    {
        $languageId = $this->notEmpty('language_id');
        $questions = LanguageQuestionModel::getQuestion($languageId);
        if (!$questions) {
            $this->errorMsg(16000);
        }
        $question = array_column($questions, 'question');
        $this->success('', compact('question'));
    }

    /**
     * 创建需求
     */
    public function create(NeedQuestionModel $needQuestionModel, NeedQuestionValidate $needQuestionValidate)
    {
        $this->isPost();
        $data = $this->request->post();
        $this->paramValidate($needQuestionValidate, $data);
        $userId = $this->userId;
        $needQuestion = NeedQuestionModel::getByUserId($data['language_id'], $userId);
        if ($needQuestion) {
            $this->errorMsg(10000);
        }
        $data['user_id'] = $userId;
        $data['title'] = json_encode($data['title'], JSON_UNESCAPED_UNICODE);

        $res = $needQuestionModel->add($data);
        $res ? $this->success() : $this->forbidden();

    }

    /**
     * 搜索
     */
    public function search()
    {
        //老师或语种名称
        $searchName = $this->notEmpty('search_name');
        $size = $this->notEmpty('size');
        $data = $this->request->get();
        //老师筛选条件
        $teacherCourse = SearchService::search($data);
        //语种等级
        $grade = LanguageGradeModel::getBySearch($searchName);
        $this->success('', [
            'teacher_course' => $teacherCourse->items(),
            'total' => $teacherCourse->total(),
            'grade' => $grade
        ]);
    }

    /**
     * 编辑需求问卷
     */
    public function edit(NeedQuestionValidate $needQuestionValidate)
    {
        $this->isPost();
        $needId = $this->notEmpty('need_id');
        $data = $this->request->post();
        $this->paramValidate($needQuestionValidate, $data);
        $userId = $this->userId;
        $needQuestion = NeedQuestionModel::find($needId);
        if (!$needQuestion) {
            $this->errorMsg(10005);
        }
        $data['user_id'] = $userId;
        $data['title'] = json_encode($data['title'], JSON_UNESCAPED_UNICODE);
        if ($needQuestion->title == $data['title']) {
            $this->success();
        }
        $res = $needQuestion->edit($data);
        $res ? $this->success() : $this->forbidden();
    }

}
